﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

using perfume.Models.DB;
using perfume.Models.ObjectManager;
using perfume.Models.ViewModels;
using System.Web.Security;

namespace perfume.Controllers
{
    public class AccountController : Controller
    {
        // GET: /Account/LogIn
        perfume_dbEntities per_db = new perfume_dbEntities();

        public ActionResult LogOn()
        {
            return View();
        }

        //
        // POST: /Account/LogIn

        [HttpPost]
        public ActionResult LogOn(UserLogIn model, string returnUrl)
        {
            if (ModelState.IsValid)
            {
                UserManager userManager = new UserManager();
                string password = userManager.GetUserPassword(model.UserName);
                if (string.IsNullOrEmpty(password))
                {

                    ModelState.AddModelError("", "The user login or password provided is incorrect.");
                }

                if (model.Password == password)
                {
                    //Session["has_logout"] = 0;
                    Session["username"] = model.UserName;
                    FormsAuthentication.SetAuthCookie(model.UserName, false);
                    if (Url.IsLocalUrl(returnUrl) && returnUrl.Length > 1 && returnUrl.StartsWith("/")
                        && !returnUrl.StartsWith("//") && !returnUrl.StartsWith("/\\"))
                    {
                        
                        
                        return Redirect(returnUrl);
                    }
                    else
                    {
                        
                        return RedirectToAction("Index", "Home");
                    }
                }
                else
                {
                    ModelState.AddModelError("", "The password provided is incorrect.");
                }
            }

            // If we got this far, something failed, redisplay form
            return View(model);
        }

        // GET: /Account/LogOff 

        public ActionResult LogOut()
        {
            FormsAuthentication.SignOut();
            Session["cart_count"] = "0";

            //Session["has_logout"] = 1;

            var cart = ShoppingCart.GetCart(this.HttpContext);
            cart.EmptyCart();

            return RedirectToAction("Index", "Home");
        }

        // GET: /Account/SignUp
        public ActionResult SignUp()
        {
            return View("SignUp");
        }

        // POST: /Account/SignUp
        [HttpPost]
        public ActionResult SignUp(UserView userview)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    UserManager userManager = new UserManager();
                    if (!userManager.IsUserLoginIDExist(userview.UserName))
                    {
                        userManager.Add(userview);
                        //FormsAuthentication.SetAuthCookie("admin", true);
                        return RedirectToAction("Index", "Home");
                    }
                    else
                    {
                        ModelState.AddModelError("", "LogID already taken");
                    }
                }
            }
            catch
            {
                return View(userview);
            }

            return View(userview);
        }

    }
}
